<script setup lang="ts">
import { ref, reactive } from 'vue';
import * as admin from '@/api/login.js';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import type { FormInstance, FormRules } from 'element-plus';

interface LoginInfo {
    username: string;
    password: string;
    confirmPassword: string;
    phone: string;
}

const router = useRouter();
const loginForm = ref<FormInstance>();

const lgStr = localStorage.getItem('login-param');
const defParam = lgStr ? JSON.parse(lgStr) : null;

const param = reactive<LoginInfo>({
    username: defParam ? defParam.username : '',
    password: '',
    confirmPassword: '',
    phone: '',
});

// 校验规则
const rules: FormRules = {
    username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 6, message: '密码长度至少6位', trigger: 'blur' },
    ],
    confirmPassword: [
        { required: true, message: '请再次输入密码', trigger: 'blur' },
        { 
            validator: (rule, value, callback) => {
                if (value !== param.password) {
                    callback(new Error('两次密码输入不一致'));
                } else {
                    callback();
                }
            }, 
            trigger: 'blur' 
        },
    ],
    phone: [
        { required: true, message: '请输入联系电话', trigger: 'blur' },
        { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' },
    ],
};

// 提交表单
const submitForm = async () => {
    if (!loginForm.value) return;
    
    try {
        await loginForm.value.validate(); // 校验表单
        console.log('提交数据:', param);

        let result = await admin.register(param);
        if (result.code == 0) {
            ElMessage.success(result.msg);
            router.push('/login');
        } else {
            ElMessage.error(result.msg);
        }
    } catch (error) {
        console.log('表单校验未通过', error);
    }
};
</script>

<template>
    <div class="login-bg">
        <div class="login-container">
            <div class="login-header">
                <img class="logo mr10" src="../assets/img/logo.svg" alt="" />
                <div class="login-title">小区维系同后台管理平台</div>
            </div>
            <el-form :model="param" :rules="rules" ref="loginForm" size="large">
                <el-form-item prop="username">
                    <el-input v-model="param.username" placeholder="用户名">
                        <template #prepend><el-icon><User /></el-icon></template>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input type="password" placeholder="密码" v-model="param.password">
                        <template #prepend><el-icon><Lock /></el-icon></template>
                    </el-input>
                </el-form-item>
                <el-form-item prop="confirmPassword">
                    <el-input type="password" placeholder="确认密码" v-model="param.confirmPassword">
                        <template #prepend><el-icon><Lock /></el-icon></template>
                    </el-input>
                </el-form-item>
                <el-form-item prop="phone">
                    <el-input placeholder="联系电话" v-model="param.phone">
                        <template #prepend><el-icon><Phone /></el-icon></template>
                    </el-input>
                </el-form-item>

                <el-button class="login-btn" type="primary" size="large" @click="submitForm">注册</el-button>
                <p class="login-text">
                    已有账号？<el-link type="primary" @click="router.push('/login')">登录</el-link>
                </p>
            </el-form>
        </div>
    </div>
</template>




<style scoped>
.login-bg {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100vh;
    background: url(../assets/img/login-bg.jpg) center/cover no-repeat;
}

.login-header {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 40px;
}

.logo {
    width: 35px;
}

.login-title {
    font-size: 22px;
    color: #333;
    font-weight: bold;
}

.login-container {
    width: 450px;
    border-radius: 5px;
    background: #fff;
    padding: 40px 50px 50px;
    box-sizing: border-box;
}

.pwd-tips {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 14px;
    margin: -10px 0 10px;
    color: #787878;
}

.pwd-checkbox {
    height: auto;
}

.login-btn {
    display: block;
    width: 100%;
}

.login-tips {
    font-size: 12px;
    color: #999;
}

.login-text {
    display: flex;
    align-items: center;
    margin-top: 20px;
    font-size: 14px;
    color: #787878;
}
</style>
